package com.bsy.videostation.ui.fragment

import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bsy.framework.base.BaseMvvmFragment
import com.bsy.framework.ext.onClick
import com.bsy.framework.helper.AppHelper
import com.bsy.videostation.adapter.HistoryAdapter
import com.bsy.videostation.adapter.lisener.OnItemClickListener
import com.bsy.videostation.model.VideoEntity
import com.bsy.videostation.ui.activity.WebViewActivity
import com.bsy.videostation.view_type.HomeViewType
import com.bsy.videostation.viewmodel.HistoryViewModel
import com.bsy.videostation.viewmodel.MainViewModel
import com.dg.videostation.databinding.FragmentHistoryBinding
import me.jessyan.autosize.AutoSize
import me.jessyan.autosize.internal.CustomAdapt

/**
 * Created by since on 2024/6/2.
 * Copyright
 * 描述：
 */
class HistoryFragment : BaseMvvmFragment<FragmentHistoryBinding, HistoryViewModel>(), CustomAdapt {
    private val nextWeekAdapter: HistoryAdapter by lazy {
        HistoryAdapter()
    }
    private val prevWeekAdapter: HistoryAdapter by lazy {
        HistoryAdapter()
    }
    private var mainViewModel: MainViewModel? = null
    override fun initViewModel() {
        super.initViewModel()
        mainViewModel = AppHelper.getApplicationViewModel(MainViewModel::class.java)
    }

    override fun initView(view: View, savedInstanceState: Bundle?) {
        initRv()
    }

    override fun initData() {
        mViewModel.getHistoryData()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        AutoSize.autoConvertDensity(activity, 1366F, true)
        return getContentView(inflater, container)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        AutoSize.autoConvertDensity(activity, 1366F, true)
        super.onConfigurationChanged(newConfig)
    }

    override fun initObserver() {
        super.initObserver()
        mViewModel.historyOneWeekData.observe(viewLifecycleOwner) {
            prevWeekAdapter.setList(mainViewModel?.buildAdapterData(it, false))
        }
        mViewModel.historyAfterOneWeekData.observe(viewLifecycleOwner) {
            nextWeekAdapter.setList(mainViewModel?.buildAdapterData(it, false))
        }
    }

    private fun initRv() {
        mBinding?.ivBack?.onClick {
            mainViewModel?.historyEvent?.value = HomeViewType.HOME_PAGE.value
        }
        mBinding?.rvWeekNext?.apply {
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            this.setHasFixedSize(true)
            this.adapter = nextWeekAdapter
        }
        mBinding?.rvWeekPrev?.apply {
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            this.setHasFixedSize(true)
            this.adapter = prevWeekAdapter
        }
    }

    override fun initListener() {
        prevWeekAdapter.setCustomOnItemClickListener(object : OnItemClickListener<VideoEntity> {
            override fun onClick(position: Int, item: VideoEntity) {
                toDetailPage(item)
            }


        })
        nextWeekAdapter.setCustomOnItemClickListener(object : OnItemClickListener<VideoEntity> {
            override fun onClick(position: Int, item: VideoEntity) {
                toDetailPage(item)
            }
        })
    }

    private fun toDetailPage(item: VideoEntity) {
        if (item.detail.isNullOrEmpty()) {
            showToast("No play address found")
            return
        }
        val intent = Intent(context ?: requireContext(), WebViewActivity::class.java)
        val bundle = Bundle().apply {
            putString("url", item.detail)
        }
        intent.putExtras(bundle)
        startActivity(intent)
    }

    companion object {
        fun newInstance() = HistoryFragment()
    }

    override fun isBaseOnWidth(): Boolean {
        return true
    }

    override fun getSizeInDp(): Float {
        return 1366F
    }
}